﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Asplode.GameEntity.Characters;
using BEPUphysics.Entities;

namespace Asplode.GameWorld
{
    public class GameWorldEventHandler
    {

        private GameWorld GameWorld;
        public GameWorldEventHandler(GameWorld gw)
        {
            GameWorld = gw;
        }

        public void subscribe(EventDispatcher EventDispatcher)
        {
            EventDispatcher.BoxSpawn += new EventDispatcher.BoxSpawnHandler(EventDispatcher_BoxSpawn);
            EventDispatcher.HumanSpawn += new EventDispatcher.HumanSpawnHandler(EventDispatcher_HumanSpawn);
            EventDispatcher.RemoveEntity += new EventDispatcher.RemoveEntityHandler(EventDispatcher_RemoveEntity);
            EventDispatcher.ZombieSpawn += new EventDispatcher.ZombieSpawnHandler(EventDispatcher_ZombieSpawn);
        }        

        void EventDispatcher_BoxSpawn(Box box)
        {
            GameWorld.EntList.Add(box);
        }

        void EventDispatcher_HumanSpawn(Entity entity)
        {
            GameWorld.EntList.Add(entity);
        }

        void EventDispatcher_RemoveEntity(Entity entity)
        {
            foreach (Human player in GameWorld.Humans)
            {
                if (player.gameObject.body == entity)
                {
                    GameWorld.Humans.Remove(player);
                }
            }
            foreach (Zombie zombie in GameWorld.Zombies)
            {
                if (zombie.gameObject.body == entity)
                {
                    GameWorld.Zombies.Remove(zombie);
                }
            }
            if (GameWorld.EntList.Contains(entity))
            {
                GameWorld.EntList.Remove(entity);
            }
        }

        void EventDispatcher_ZombieSpawn(Entity entity)
        {
            GameWorld.EntList.Add(entity);
        }        
    }
}
